<%include header%>
<!--socket.io-->
<script type="text/javascript" src="http://cdn.staticfile.org/socket.io/1.3.7/socket.io.js"></script>

<div style="height: 20px;"></div>
<div class="jumbotron">
	<%if(!users){%>
	  <h1>欢迎来到中兴软件技术公司微博</h1>
	  <p>中兴软件公司微博是一个基于node.js的微博系统.</p>
	  <p>当前会员人数 : <%=num%></p>
	  <p><a href="/before/login" class="btn btn-primary btn-large">登陆</a>
	    <label>&nbsp;</label><a href="/before/reg" class="btn btn-default btn-large">立即注册</a>
	  </p>
	<%}else{%>
		<h2 class="index-h2">欢迎&nbsp;<%=users.name%>&nbsp;来到中兴软件技术公司微博</h2>
		<p>当前会员人数 : <%=num%></p>
		<!-- <form class="form-horizontal" action="/before/release" method="post"> -->
			<div class="form-group">
			  <div class="col-sm-11">
			    <input id="connect" type="text" name="connect" class="form-control"/>
			  </div>
			  <div class="col-sm-1">
			    <button id="submit" type="submit" class="btn btn-success">发布</button>
			  </div>
			</div>
		<!-- </form> -->
  <%}%>
</div>
<div id="microblogrows" class="row">
	<%for(var i=0;i< microblogs.length;i++){ var microblog = microblogs[i]%>
		<div class="col-sm-12">
	    <h3><image style="border:1px solid red;border-radius: 4px 4px 4px 4px" src="<%=microblog.userid.headPicture%>" width="50px" height="50px" alt="zhao" onerror="this.src='/images/<%=microblog.userid.random%>.jpg'"/><%=microblog.userid.name%>∶<%=microblog.createDate%> 说</h3>
	    <p><%=microblog.connect%></p>
	  </div>
	<%}%>
</div>
<script type="text/javascript">
	$("#submit").click(function(){
		var connect = $("#connect").val();
		$.ajax({
		  type: "POST",
		  url: '/before/release',
		  dataType: "json",
		  contentType: "application/x-www-form-urlencoded; charset=utf-8",
		  data: {connect: connect},
		  success: function (data) {

		  	if (data && data.result=='ok') {
		  		$("#connect").val('');
		  		socket.emit('click');
		  	};

		  	if (data && data.result=='no') {
		  		alert('好像出了点小问题');
		  	};

		  	if (data && data.result=='null') {
		  		alert('说点什么吧');
		  	}
		  },
		  error: function () {
		    alert('提交出错');
		  }
		});  
	});

	var socket = io();
	socket.on('microblog', function (data) {
		var microbloghtml = '';
		var microblogs = data.microblogs;

		for (var i = 0; i < microblogs.length; i++) {

			var microblog = microblogs[i];
			microbloghtml+='<div class="col-sm-12">'+
	    '<h3><image style="border:1px solid red;border-radius: 4px 4px 4px 4px" '+
	    'src="'+microblog.userid.headPicture+'" width="50px" height="50px" alt="zhao" '+
	    'onerror="this.src=\'/images/'+microblog.userid.random+'.jpg\'"/>'+microblog.userid.name+'∶'+
	    microblog.createDate+' 说</h3><p id="microblog'+i+'"></p></div';
		};

		$("#microblogrows").html(microbloghtml);
		
		for (var i = 0; i < microblogs.length; i++) {
			var microblog1 = microblogs[i];
			$("#microblog"+i).text(microblog1.connect);
		}
	})
</script>
<%include footer%>